package main

import "fmt"
import "math/rand"
import "time"

func main() {

	var randNumber int
	// step01 create
	createNumber(&randNumber)

	// step02 getNo
	randSlice := make([]int, 4)
	getNumber(randSlice, randNumber)
	fmt.Println(" randSilce = ", randSlice)

	// step03 onGame
	fmt.Print(" start game : \n")
	onGame(randSlice)
	fmt.Print(" end game ! ")

}

func createNumber(pointer *int) {
	rand.Seed(time.Now().UnixNano())
	var number int
	for {
		number = rand.Intn(10000) // 保证4位数
		if number >= 1000 {
			break
		}
	}
	fmt.Println(" number = ", number)
	*pointer = number
}

func getNumber(randSilce []int, randNo int) {
	randSilce[0] = randNo / 1000
	randSilce[1] = randNo % 1000 / 100
	randSilce[2] = randNo % 100 / 10
	randSilce[3] = randNo % 10
}

func onGame(randSilce []int) {
	var number int
	for {
		for {
			fmt.Printf(" please input 4 size number : ")
			fmt.Scan(&number)
			if number < 10000 && number > 999 {
				break
			}
			fmt.Println(" not proper number : ", number)
		}
		fmt.Println(" proper number : ", number, ".... waiting for the result \n ")
		keySlice := make([]int, 4)
		getNumber(keySlice, number)
		right := 0
		for j := 0; j < len(keySlice); j++ {
			if keySlice[j] > randSilce[j] {
				fmt.Printf("\t 第 %d 位 数字大了 \n ", j+1)
			} else if keySlice[j] < randSilce[j] {
				fmt.Printf("\t 第 %d 位 数字小了 \n ", j+1)
			} else {
				fmt.Printf("\t 恭喜您，第 %d 位 数字猜对了 \n ", j+1)
				right++
			}
		}
		if right == len(keySlice) {
			fmt.Println("太牛逼了，全猜对了！")
			break
		}
	}

}
